﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework.Media;
using System.Collections.Generic;
using System.IO;
using Microsoft.Devices;

namespace WindowsPhoneDataBoundApplication1.ViewModels
{
    public class ImageGalleryViewModel : INotifyPropertyChanged
    {
        public ImageGalleryViewModel()
        {
            this.Items = new ObservableCollection<PictureAlbumViewModel>();
            Initialize();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<PictureAlbumViewModel> Items { get; private set; }
        public bool IsDataLoaded
        {
            get;
            private set;
        }
        private bool Initialized { get; set; }
        private void Initialize()
        {
            if (Initialized)
                return;
            // MediaLibrary IDisposable so using a using    
            using (MediaLibrary myMediaLibrary = new MediaLibrary())
            {
                // Check we're on the emulator and haven't already added images       
                if (Microsoft.Devices.Environment.DeviceType == DeviceType.Emulator &&
                    myMediaLibrary.Pictures.Count == 0)
                {
                    // Here's our list of image filenames           
                    List<string> photoList = new List<string>(){
                        "Phone1.png",
                        "Phone2.png",               
                        "Phone3.png",                
                        "Phone4.png",               
                        "Phone5.png",
                        "Phone6.png"
                    };
                    // Enumerate the list           
                    foreach (var item in photoList)
                    {                // Create a URI with the folder name and filename              
                        Uri myUri = new Uri(string.Format(@"Images/{0}", item), UriKind.Relative);
                        // Access the resource in the application package and save               
                        // the image to the media library               
                        using (Stream photoStream = App.GetResourceStream(myUri).Stream)
                        {                            
                            myMediaLibrary.SavePicture(item, photoStream);
                        }
                    }
                }
            }
            Initialized = true;
        }

        public void LoadData()
        {
            if (IsDataLoaded)
                return;
            if (!Initialized) 
                Initialize();
            MediaLibrary library = new MediaLibrary();

            foreach (PictureAlbum p in library.RootPictureAlbum.Albums)
            {
                Items.Add(new PictureAlbumViewModel() { Name = p.Name, Count = p.Pictures.Count });
            }
            IsDataLoaded = true;
            
        }
        
        #endregion
    }
}
